package org.example;

import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.example.controller.*;
import org.example.dao.SettingsRepository;
import org.example.init.CustomSplash;
import org.example.model.SettingsEntity;
import org.example.service.PanelMainService;
import org.example.service.StartApplicationService;
import org.example.view.JavaProcessManagementView;
import org.example.view.MainStageView;
import org.example.view.PlanAddDialogView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.IOException;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2020/11/26 9:46
 */
@EnableScheduling
@SpringBootApplication
public class Main extends AbstractJavaFxApplicationSupport {
    private static Stage primaryStage;

    public static void main(String[] args) {
        launch(Main.class, MainStageView.class, new CustomSplash(), args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        primaryStage = stage;
        primaryStage.setTitle("java进程管理");
        super.start(stage);
        stage.setOnCloseRequest(event->{
            System.exit(0);
        });
    }

    /**
     * 页面加载之后
     * @param stage
     * @param ctx
     */
    @Override
    public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
        super.beforeInitialView(stage, ctx);
        PlanAddDialogController.applicationContext = ctx;
        AppProcessAddDialogController.applicationContext = ctx;
        SettingsDialogViewController.applicationContext = ctx;
        System.out.println("beforeInitialView执行");
        //主界面
        MainStageView mainStageView = ctx.getBean(MainStageView.class);

        //进程页面
        JavaProcessManagementView javaProcessManagementView = ctx.getBean(JavaProcessManagementView.class);
        JavaProcessManagementController javaProcessManagementController = ctx.getBean(JavaProcessManagementController.class);
        BorderPane mainStageViewBorderPan = (BorderPane) mainStageView.getView();
        mainStageViewBorderPan.setCenter(javaProcessManagementView.getView());
        SettingsRepository settingsRepository = ctx.getBean(SettingsRepository.class);
        StartApplicationService startApplicationService = ctx.getBean(StartApplicationService.class);
        this.startAll(settingsRepository,startApplicationService);

        PanelMainService panelMainService = ctx.getBean(PanelMainService.class);
        panelMainService.appRunTime();


    }

    public void startAll(SettingsRepository settingsRepository, StartApplicationService startApplicationService){
        //是否需要加载项目
        List<SettingsEntity> all = settingsRepository.findAll();
        if(all != null && all.size()>0){
            //判断是否需要加载
            int isAutoLoad = all.get(0).getIsAutoLoad();
            if(isAutoLoad == 1){
                //需要启动，调用启动全部方法
                Loading loading = new Loading(Main.getStage());
                loading.show();
                JavaProcessManagementController.executorService.execute(()->{
                    int i = startApplicationService.startAll(loading);
                    loading.closeStage();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    /*Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setTitle("提示");
                    alert.setHeaderText("启动完成");
                    alert.setContentText("请等待程序加载完成");
                    alert.showAndWait();*/
                });
            }
        }
    }

    /**
     * 页面加载之前
     * @param splashStage
     */
    @Override
    public void beforeShowingSplash(Stage splashStage) {
        super.beforeShowingSplash(splashStage);
        System.out.println("beforeShowingSplash执行");


    }


}
